home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / wheels1.arc / GETINTGR.LIB < prev    next >
Text File  |  1985-06-28  |  3KB  |  76 lines

  1. {@@@@@@@@@@@ copyright (C) 1984 by Neil J. Rubenking @@@@@@@@@@@@@@@@@@@@@@@@
  2. The purchaser of these procedures and functions may include them in COMPILED
  3. programs freely, but may not sell or give away the source text.
  4.  
  5.      Use this function to take an integer as input without risking
  6.      the user crashing your program by entering an invalid value.
  7.      You specify the allowable range.
  8.  
  9. {@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}
  10. function GetInteger(min, max : integer):integer;
  11. var
  12.   onedigit : char;
  13.   temp  : real;
  14.   good, minus, done : boolean;
  15.   PosX, PosY, digits : byte;
  16. begin
  17.   PosX := WhereX; PosY := WhereY;  { hold the starting location -- if the
  18.                                      person entering data makes an error,
  19.                                      come back to this location and blank
  20.                                      out six character spaces.}
  21.   repeat
  22.     GotoXY(PosX,PosY);
  23.     Write('      ');
  24.     gotoXY(PosX,PosY);
  25.     digits := 0;
  26.     temp   := 0;
  27.     good   := true;
  28.     done   := false;
  29.     repeat
  30.       repeat until keypressed;    { take characters from the keyboard }
  31.       read(Kbd, onedigit);
  32.       digits := digits + 1;
  33.       if digits = 1 then              { if the FIRST character is a "-" }
  34.           if OneDigit = '-' then      { then the number is negative     }
  35.             begin
  36.               minus := true;
  37.               Write('-');
  38.             end
  39.           else minus := false;
  40.       case OneDigit of
  41.         '0'..'9' : begin
  42.                      temp := 10*temp + ord(OneDigit) - 48;
  43.                      write(OneDigit);            { if the key pressed is a   }
  44.                    end;                          { number, write it on the   }
  45.         #8       : begin                         { screen and add it to      }
  46.                      Write(#8,#32,#8);           { "temp".  If it's a Back   }
  47.                      temp := trunc(temp) div 10; { Space, blank out the last }
  48.                      digits := digits - 2;       { digit and remove it from  }
  49.                    end;                          { "temp".  If it's <return> }
  50.         #13      : done := true;                 { you're done.              }
  51.       end;                                       
  52.       if digits > 6 then done := true;  { integers can be up to 32,767,
  53.                                           a maximum of five digits, plus
  54.                                           one for a possible minus sign.}
  55.       if minus then temp := -temp;  { make temp negative for comparisons }
  56.       if (temp < min) and (done or minus) then
  57.         begin
  58.           good := false;
  59.           done := true;
  60.         end;
  61.       if (temp > max) and (done or (not minus)) then
  62.         begin
  63.           good := false;
  64.           done := true;
  65.         end;
  66.       if minus then temp := - temp; { set temp back to positive for adding
  67.                                       more digits.}
  68.     until done;
  69.   until good and done;
  70.   if minus then temp := -temp;
  71.   GetInteger := trunc(temp);
  72. end;
  73. {@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}
  74.  
  75.  
  76.